﻿Imports Base

Public Class ActionHub
    Inherits MarshalByRefObject
    Implements Base.IPhlogios

    Private ConnectionString As String

    Sub New()
        Me.ConnectionString = My.Settings.ConnectionString
    End Sub

    'Public Function Staff(ByVal aMoira As Base.Carrier.Moira, ByVal aTypos As Base.Carrier.Typos) As Object Implements Base.IPhlogios.Staff
    '    Dim dc As New Base.PhlogiosDataContext(ConnectionString)
    '    Dim list = From b In dc.Balances Join s In dc.Stratefsimos On b.id_Stratefsimos Equals s.Id
    '    Return list
    'End Function

    Public Function Moires() As Base.Entities.Moira() Implements Base.IPhlogios.Moires
        Dim dc As New Base.PhlogiosDataContext(ConnectionString)
        Dim result As New List(Of Base.Entities.Moira)
        For Each m In dc.Moiras.ToList
            result.Add(New Base.Entities.Moira(m.Id, m.Name))
        Next
        Return result.ToArray
    End Function

    Public Function ServiceType() As Base.Entities.ServiceType() Implements Base.IPhlogios.ServiceType
        Dim dc As New Base.PhlogiosDataContext(ConnectionString)
        Dim result As New List(Of Base.Entities.ServiceType)
        For Each m In dc.Typos.ToList
            result.Add(New Base.Entities.ServiceType(m.id_Typos, m.Typos))
        Next
        Return result.ToArray
    End Function

    Public Function Staff(ByVal aMoira() As Base.Entities.Moira, ByVal aTypos() As Base.Entities.ServiceType) As SortableCollection(Of Entities.Dues) Implements Base.IPhlogios.Staff
        Dim dc As New Base.PhlogiosDataContext(ConnectionString)

        dc.Log = Console.Out

        Dim moiresIds As New List(Of Integer)
        For Each m In aMoira
            moiresIds.Add(m.Id)
        Next

        Dim typoiIds As New List(Of Integer)
        For Each t In aTypos
            typoiIds.Add(t.Id)
        Next


        Dim result As New SortableCollection(Of Base.Entities.Dues)

        Dim resQ = From b In dc.Balances Join s In dc.Stratefsimos On s.id_Stratefsimos Equals b.id_Stratefsimos _
                   Join t In dc.Typos On b.id_Typos Equals t.id_Typos _
                   Where moiresIds.Contains(s.Moira.Id) And typoiIds.Contains(b.id_Typos)


        For Each rr In resQ.ToArray
            result.Add(New Base.Entities.Dues( _
                       rr.s.Onoma, rr.s.Eponimo, _
                       rr.s.Moira.Name, rr.s.ASMA, _
                       rr.t.Typos, rr.b.Provlepomenes, rr.b.Pragmatikes, rr.s.id_Stratefsimos))
        Next
        Return result
    End Function

    Public Function StaffSelection(ByVal aMoira() As Base.Entities.Moira) As SortableCollection(Of Entities.Staff) Implements Base.IPhlogios.StaffSelection
        Dim dc As New Base.PhlogiosDataContext(ConnectionString)

        Dim moiresIds As New List(Of Integer)
        For Each m In aMoira
            moiresIds.Add(m.Id)
        Next

        Dim strQ = From s In dc.Stratefsimos _
                   Where moiresIds.Contains(s.id_Moira) _
                   And s.isDeleted = False

        Dim result As New SortableCollection(Of Base.Entities.Staff)
        result.AllowNew = True
        For Each s In strQ
            result.Add(New Base.Entities.Staff(s.id_Stratefsimos, s.Onoma, s.Eponimo, s.Moira.Id))
        Next

        Return result
    End Function

    Public Function Provlepomenes(ByVal aMoira() As Base.Entities.Moira) As SortableCollection(Of Entities.StaffProvlepe) Implements Base.IPhlogios.Provlepomenes
        Dim dc As New Base.PhlogiosDataContext(ConnectionString)
        dc.Log = System.Console.Out

        Dim moiresIds As New List(Of Integer)
        For Each m In aMoira
            moiresIds.Add(m.Id)
        Next

        Dim provQ = From s In dc.Stratefsimos Join p In dc.Provlepomenis On s.id_Stratefsimos Equals p.id_Stratefsimos _
                      Where moiresIds.Contains(s.id_Moira)

        Dim result As New SortableCollection(Of Base.Entities.StaffProvlepe)
        For Each sp In provQ
            result.Add(New Base.Entities.StaffProvlepe(sp.p.id_Provlepomenes, sp.s.id_Stratefsimos, sp.p.id_Typos, sp.p.Yphresies))
        Next

        Return result
    End Function

    Public Function Programmatismenes(ByVal aMoira() As Base.Entities.Moira) As SortableCollection(Of Entities.StaffProgram) Implements Base.IPhlogios.Programmatismenes
        Dim dc As New Base.PhlogiosDataContext(ConnectionString)
        dc.Log = System.Console.Out

        Dim moiresIds As New List(Of Integer)
        For Each m In aMoira
            moiresIds.Add(m.Id)
        Next

        Dim progQ = From s In dc.Stratefsimos Join p In dc.Programmatismenis On s.id_Stratefsimos Equals p.id_Stratefsimos _
                    Where moiresIds.Contains(s.id_Moira)

        Dim result As New SortableCollection(Of Base.Entities.StaffProgram)
        For Each sp In progQ
            result.Add(New Base.Entities.StaffProgram(sp.p.id_Prog, sp.s.id_Stratefsimos, sp.p.id_Typos, sp.p.Hmeromhnia))
        Next

        Return result
    End Function

    Public Sub UpdateStaff(ByVal newStaff As SortableCollection(Of Base.Entities.Staff), ByVal deleteStaff As Integer()) Implements Base.IPhlogios.UpdateStaff
        Dim dc As New Base.PhlogiosDataContext(ConnectionString)

        For Each s In newStaff
            Dim id As Integer = s.Id
            Dim isNew As Boolean = False
            'Dim newStrQ = dc.Stratefsimos.Where(Function(ds) ds.Id = id)
            Dim newStrQ = From ds In dc.Stratefsimos _
                Where ds.id_Stratefsimos = id
            Dim newSta = newStrQ.FirstOrDefault
            If newSta Is Nothing Then
                newSta = New Base.Carrier.Stratefsimos()
                isNew = True
            End If
            newSta.Onoma = s.Onoma
            newSta.Eponimo = s.Eponymo
            newSta.id_Moira = s.Moira
            If isNew Then dc.Stratefsimos.InsertOnSubmit(newSta)
        Next

        Dim deleQ = From ds In dc.Stratefsimos _
                    Where deleteStaff.Contains(ds.id_Stratefsimos) _
                    And ds.isDeleted = False

        For Each dele In deleQ
            dele.isDeleted = True
        Next

        dc.SubmitChanges()
    End Sub

    Public Sub UpdateProvlepomenes(ByVal newProvlepomenes As Base.SortableCollection(Of Base.Entities.StaffProvlepe), ByVal deleteProvlepomenes() As Integer) Implements Base.IPhlogios.UpdateProvlepomenes
        Dim dc As New Base.PhlogiosDataContext(ConnectionString)

        For Each s In newProvlepomenes
            Dim id As Integer = s.Id
            Dim isNew As Boolean = False
            Dim newStrQ = From ds In dc.Provlepomenis _
                Where ds.id_Provlepomenes = id
            Dim newSta = newStrQ.FirstOrDefault
            If newSta Is Nothing Then
                newSta = New Base.Carrier.Provlepomeni()
                isNew = True
            End If
            newSta.id_Stratefsimos = s.StaffId
            newSta.id_Typos = s.ServiceType
            newSta.Yphresies = s.Provlepomenes
            If isNew Then dc.Provlepomenis.InsertOnSubmit(newSta)
        Next

        Dim deleQ = From ds In dc.Provlepomenis _
                    Where deleteProvlepomenes.Contains(ds.id_Provlepomenes)

        For Each dele In deleQ
            dc.Provlepomenis.DeleteOnSubmit(dele)
        Next

        dc.SubmitChanges()

    End Sub
End Class
